<?php
namespace app\model;

use think\Model;
use think\facade\Db;

class User extends Model
{

    protected $name = 'user';
    
    // 设置字段信息
    protected $schema = [
        'id'          => 'int',
        'username'    => 'string',
        'password'    => 'string',
        'name'        => 'string',
        'email'       => 'string',
        'phone'       => 'string',
        'avatar'      => 'string',
        'department'  => 'string',
        'position'    => 'string',
        'status'      => 'tinyint',
        'last_login_time' => 'datetime',
        'create_time' => 'datetime',
        'update_time' => 'datetime',
        'delete_time' => 'datetime',
    ];
    
    // 开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    
    // 软删除
    use \think\model\concern\SoftDelete;
    protected $deleteTime = 'delete_time';
    
    // 隐藏属性
    protected $hidden = ['password'];
    
    // 关联角色
    public function roles()
    {
        return $this->belongsToMany(Role::class, 'user_role');
    }
    
    // 密码修改器
    public function setPasswordAttr($value)
    {
        return password_hash($value, PASSWORD_BCRYPT);
    }
    
    // 状态获取器
    public function getStatusTextAttr()
    {
        $status = [
            0 => '禁用',
            1 => '正常'
        ];
        return $status[$this->status] ?? '未知';
    }
    
    // 检查用户是否有指定权限
    public function hasPermission($permissionCode)
    {
        $roles = $this->roles()->with(['permissions' => function($query) use ($permissionCode) {
            $query->where('code', $permissionCode)->where('status', 1);
        }])->select();
        
        foreach ($roles as $role) {
            if ($role->permissions->isNotEmpty()) {
                return true;
            }
        }
        
        return false;
    }
    
    // 获取用户所有权限
    public function getPermissions()
    {
        $permissions = [];
        $roles = $this->roles()->with('permissions')->select();
        //var_dump($roles);die;
        foreach ($roles as $role) {
            foreach ($role->permissions as $permission) {
                if ($permission->status == 1) {
                    $permissions[$permission->id] = [
                        'id' => $permission->id,
                        'name' => $permission->name,
                        'code' => $permission->code,
                        'type' => $permission->type,
                        'path' => $permission->path,
                        'component' => $permission->component,
                        'icon' => $permission->icon,
                        'parent_id' => $permission->parent_id
                    ];
                }
            }
        }
        
        return array_values($permissions);
    }
    
    // 检查用户密码
    public function checkPassword($password)
    {
        return password_verify($password, $this->password);
    }
    
    // 搜索用户名
    public function searchUsernameAttr($query, $value)
    {
        if (!empty($value)) {
            $query->where('username', 'like', '%' . $value . '%');
        }
    }
    
    // 搜索用户状态
    public function searchStatusAttr($query, $value)
    {
        if (is_numeric($value)) {
            $query->where('status', $value);
        }
    }
    
    // 搜索部门
    public function searchDepartmentAttr($query, $value)
    {
        if (!empty($value)) {
            $query->where('department', $value);
        }
    }
} 